package com.smartcommunity.controller.building;

import com.smartcommunity.bean.enumeration.ResultCode;
import com.smartcommunity.bean.model.Result;
import com.smartcommunity.entity.building.*;
import com.smartcommunity.entity.village.Building;
import com.smartcommunity.log.interceptor.Log;
import com.smartcommunity.service.building.BuildingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author nieming
 * @descrition 水电气控制器
 * @date 2020/5/12
 */
@RestController
@RequestMapping(value = "/building")
@Api(value = "楼栋管理", tags = "楼栋管理")
@Slf4j
public class BuildingController {
    @Autowired
    private BuildingService buildingService;

    @PostMapping(value = "/add")
    @Log(info = "新增楼栋信息",module = "楼栋管理",function = "新增")
    @ApiOperation(value = "新增楼栋", notes = "新增楼栋信息")
    public Result add(@RequestBody BuildingAddReq buildingAddReq) {
        try {
            return buildingService.add(buildingAddReq);
        } catch (Exception e) {
            log.error("新增楼栋信息失败,param:{},error:", buildingAddReq, e);
            return Result.error(ResultCode.ERROR);
        }
    }
    @PostMapping(value = "/edit")
    @Log(info = "编辑楼栋信息",module = "楼栋管理",function = "编辑")
    @ApiOperation(value = "编辑楼栋", notes = "编辑楼栋")
    public Result edit(@RequestBody BuildingEditReq buildingEditReq) {
        try {
            return buildingService.edit(buildingEditReq);
        } catch (Exception e) {
            log.error("编辑楼栋失败,param:{},error:", buildingEditReq, e);
            return Result.error(ResultCode.ERROR);
        }
    }
    @PostMapping(value = "/list")
    @Log(info = "查询楼栋列表信息",module = "楼栋管理",function = "查询")
    @ApiOperation(value = "楼栋查询", notes = "楼栋查询")
    public Result list(@RequestBody BuildingListReq buildingListReq) {
        try {
            List<Building> buildings = buildingService.list(buildingListReq);
            return Result.ok(buildings);
        } catch (Exception e) {
            log.error("楼栋查询失败,param:{},error:", buildingListReq, e);
            return Result.error(ResultCode.ERROR);
        }
    }
    @GetMapping(value = "/detail")
    @Log(info = "查询楼栋详情",module = "楼栋管理",function = "查询")
    @ApiOperation(value = "楼栋详情查询", notes = "楼栋详情查询")
    public Result detail(@RequestParam Long buildingId) {
        try {
            BuildingDetailResp buildingDetailResp = buildingService.detail(buildingId);
            return Result.ok(buildingDetailResp);
        } catch (Exception e) {
            log.error("楼栋查询失败,param:{},error:", buildingId, e);
            return Result.error(ResultCode.ERROR);
        }
    }
    @PostMapping(value = "/exits")
    @Log(info = "查询楼栋是否存在",module = "楼栋管理",function = "查询")
    @ApiOperation(value = "查询楼栋是否存在(存在：true，不存在:false)", notes = "查询楼栋是否存在")
    public Result exits(@RequestBody BuildingExitsReq buildingExitsReq) {
        try {
            boolean result = buildingService.exits(buildingExitsReq);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("查询楼栋是否存在失败,param:{},error:", buildingExitsReq, e);
            return Result.error(ResultCode.ERROR);
        }
    }
    @GetMapping(value = "/delete")
    @Log(info = "删除楼栋信息",module = "楼栋管理",function = "删除")
    @ApiOperation(value = "删除楼栋", notes = "删除楼栋")
    public Result delete(@RequestParam Long buildingId) {
        try {
            buildingService.delete(buildingId);
            return Result.ok();
        } catch (Exception e) {
            log.error("删除楼栋失败,param:{},error:", buildingId, e);
            return Result.error(ResultCode.ERROR);
        }
    }
}
